import {
  Entity,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  PrimaryColumn,
  OneToOne,
  JoinColumn
} from "typeorm";
import { BblAttach } from "./attach.entity";
import { BblPrize } from "./prize.entity";
import { BblPrizeActivity } from "./prize.activity.entity";

/**
 * 抽奖活动编码管理表
 *
 * 该实体用于管理抽奖活动的编码，包含抽奖码、中奖信息、用户信息等数据
 * 每条记录代表一个抽奖码，可用于追踪抽奖码的使用情况及中奖信息
 */
@Entity("BBL_PRIZE_RESULT")
export class BblPrizeResult {
  /**
   * 抽奖码主键ID（业务自定义）
   */
  @PrimaryColumn({
    name: "ID",
    type: "varchar",
    length: 50,
    comment: "抽奖码主键ID（业务自定义）"
  })
  id: string;

  /**
   * 随机唯一编码（非ID）
   * 用于标识抽奖码的唯一性
   */
  @Column({
    name: "LOTTERY_CODE",
    type: "varchar",
    length: 50,
    nullable: false,
    unique: true,
    comment: "随机唯一编码（非ID）"
  })
  lotteryCode: string;

  /**
   * 抽奖码二维码附件
   * 关联到附件表中的二维码图片
   */
  @OneToOne(() => BblAttach, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "QR_CODE_ATTACHMENT_ID" })
  qrCode: BblAttach;

  /**
   * 关联的活动批次
   * 指向该抽奖码所属的活动批次
   */
  @OneToOne(() => BblPrizeActivity, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "ACTIVITY_BATCH_ID" })
  activityBatch: BblPrizeActivity;

  /**
   * 是否使用（0-未使用，1-已使用）
   * 标记抽奖码的使用状态
   */
  @Column({
    name: "IS_USED",
    type: "tinyint",
    default: 0,
    comment: "是否使用（0-未使用，1-已使用）"
  })
  isUsed: number;

  /**
   * 抽奖码使用时间
   */
  @Column({
    name: "USE_TIME",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    comment: "使用时间"
  })
  useTime: Date;

  /**
   * 兑奖人姓名
   */
  @Column({
    name: "WINNER_NAME",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "兑奖人姓名"
  })
  winnerName: string;

  /**
   * 收货人联系电话
   */
  @Column({
    name: "CONTACT_PHONE",
    type: "varchar",
    length: 20,
    nullable: true,
    comment: "收货人联系电话"
  })
  contactPhone: string;

  /**
   * 邮寄地址
   */
  @Column({
    name: "MAILING_ADDRESS",
    type: "varchar",
    length: 200,
    nullable: true,
    comment: "邮寄地址"
  })
  mailingAddress: string;

  /**
   * 邮编
   */
  @Column({
    name: "POSTCODE",
    type: "varchar",
    length: 10,
    nullable: true,
    comment: "邮编"
  })
  postcode: string;

  /**
   * 关联的奖品信息
   */
  @OneToOne(() => BblPrize, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "PRIZE_ID" })
  prize: BblPrize;

  /**
   * 是否发送奖品
   * 0-未发送，1-已发送
   */
  @Column({
    name: "IS_SHIPPED",
    type: "tinyint",
    default: 0,
    nullable: false,
    comment: "是否发送奖品（0-未发送，1-已发送）"
  })
  isShipped: number;

  /**
   * 快递类型
   * 如：顺丰、圆通、申通等
   */
  @Column({
    name: "SHIPPING_CARRIER",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "快递类型（如：顺丰、圆通、申通等）"
  })
  shippingCarrier: string;

  /**
   * 快递单号
   */
  @Column({
    name: "TRACKING_NUMBER",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "快递单号"
  })
  trackingNumber: string;

  /**
   * 快递单号附件
   * 关联到附件表中的快递单号图片
   */
  @OneToOne(() => BblAttach, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "TRACKING_ATTACHMENT_ID" })
  trackingAttachment: BblAttach;

  /**
   * 创建时间
   */
  @CreateDateColumn({
    name: "CREATED_AT",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    comment: "创建时间"
  })
  createdAt: Date;

  /**
   * 更新时间
   */
  @UpdateDateColumn({
    name: "UPDATED_AT",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    onUpdate: "CURRENT_TIMESTAMP(6)",
    comment: "更新时间"
  })
  updatedAt: Date;

  /**
   * 创建人ID
   */
  @Column({
    name: "CREATED_BY",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "创建人ID"
  })
  createdBy: string;

  /**
   * 更新人ID
   */
  @Column({
    name: "UPDATED_BY",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "更新人ID"
  })
  updatedBy: string;
}
